import 'package:flutter_bilibili/cache/hi_cache.dart';
import 'package:flutter_bilibili/const/cache_const.dart';
import 'package:flutter_bilibili/http/core/hi_net.dart';
import 'package:flutter_bilibili/http/request/base_request.dart';
import 'package:flutter_bilibili/http/request/login_request.dart';
import 'package:flutter_bilibili/http/request/register_request.dart';

class LoginDao {
  static login(String username, String password) {
    return _send(username, password);
  }

  static register(
      String username, String password, String imoocId, String orderId) {
    return _send(username, password, imoocId: imoocId, orderId: orderId);
  }

  static _send(String username, String password, {imoocId, orderId}) async {
    BaseRequest request;
    if (imoocId != null && orderId != null) {
      request = RegisterRequest();
    } else {
      request = LoginRequest();
    }
    request.add('name', username).add('pwd', password);
    if (imoocId != null) request.add('imoocId', imoocId);
    if (orderId != null) request.add('orderId', orderId ?? '');

    var res = await HiNet().fire(request);

    // 存储缓存
    if (res != null && res['code'] == 0 && res['data'] != null) {
      // todo:BOARDING_PASS为key，将res['data']存入全局缓存
      HiCache().setString(CacheKey.BOARDING_PASS as String, res['data']);
    }
    return res;
  }

  static getBoardingPass() {
    // todo:从全局缓存中读取BOARDING_PASS
    return HiCache().get(CacheKey.BOARDING_PASS as String);
  }
}
